# -*- coding: utf-8 -*-
from __future__ import unicode_literals

# Register your models here.
from django.contrib import admin
from django.contrib.auth import get_permission_codename
from django.utils.html import format_html
from django.urls import reverse
from django.contrib.admin.models import LogEntry
from typeidea import custom_site
from typeidea.BaseOwnerAdmin import BaseOwnerAdmin
from .models import Post,Category,Tag
from .adminforms import  PostAdminForm

class PostInline(admin.TabularInline):
    fields = ('title','desc')
    extra = 0 #控制额外多几个
    model = Post

@admin.register(Category)
class CategoryAdmin(BaseOwnerAdmin):
    list_display = ('name','status','is_nav','created_time','post_count')
    fields = ('name','status','is_nav')
    inlines = [PostInline,]
    def post_count(self, obj):
        return obj.post_set.count()

    post_count.short_description = '文章数量'


@admin.register(Tag)
class TagAdmin(BaseOwnerAdmin):
    list_display = ('name','status','created_time')
    fields = ('name','status')


class CategoryOwnerFilter(admin.SimpleListFilter):
    '''自定义过滤器只展示当前用户分类'''
    title = '分类过滤器'
    parameter_name = 'owner_category'

    def lookups(self, request, model_admin):
        return Category.objects.filter(owner=request.user).values_list('id','name')

    def queryset(self, request, queryset):
        category_id = self.value()
        if category_id:
            return queryset.filter(category_id=self.value())
        return queryset
PERMISSION_API='http://permission.sso.com.has_perm?user={}&perm_code'
@admin.register(Post)
class PostAdmin(BaseOwnerAdmin):
    # def has_add_permission(self, request):
    #     opts = self.opts
    #     codename = get_permission_codename('add',opts)
    #     perm_code = "%s.%s"%(opts.app_label,codename)
    #     resp = request.get(PERMISSION_API.format(request.user.username,perm_code))
    #     if resp.status_code == 200:
    #         return True
    #     else:
    #         return False

    form = PostAdminForm
    list_display = [
        'title','category','status','created_time','owner','operator'
    ]
    #配置那些字段可以作为一个连接，点击它进入编辑页面
    list_display_links = []
    list_filter = [CategoryOwnerFilter,]
    #配置搜索字段
    search_fields = ['title','category__name']

    actions_on_top = True
    actions_on_bottom = True

    #编辑页面
    save_on_top = True

    exclude = ('owner',)

    # fields = (
    #     ('category','title'),
    #     'desc',
    #     'status',
    #     'content',
    #     'tag',
    # )
    fieldsets = (
        ("基础配置",{
            'description':'基础配置描述',
            'fields':(
                ('title','category'),
                'status',
            )
        }),
        ('内容',{
            'fields':(
                'desc',
                'content'
            )
        }),
        ("额外信息",{
            'classes':('collapse',),
            'fields':('tag',)
        })
    )

    def operator(self,obj):
        return format_html(
            '<a href="{}">编辑</a>',
            reverse('admin:blog_post_change',args=(obj.id,))
        )
    operator.short_description = '操作'

    class Media:
        css = {
            'all':('https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/css/bootstrapn.css',)
        }
        js = ('https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/js/bootstrap.bundle.js',)

@admin.register(LogEntry)
class LogEntryAdmin(admin.ModelAdmin):
    list_display = ['object_repr','object_id','action_flag','user','change_message']
